package com.mxpio.erp.technology.service.impl;

import com.mxpio.erp.common.technology.entity.WorkShop;
import com.mxpio.erp.common.technology.entity.WorkTeam;
import com.mxpio.erp.common.technology.entity.WorkTeamMember;
import com.mxpio.erp.common.technology.service.WorkTeamService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.security.util.SecurityUtils;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * @author wupaa
 */
@Service
public class WorkTeamServiceImpl extends BaseServiceImpl<WorkTeam> implements WorkTeamService {

    @Override
    @Transactional
    public WorkTeam getByCurrentUser() {
        String loginUsername = SecurityUtils.getLoginUsername();
        if (StringUtils.isNotBlank(loginUsername)) {
            List<WorkTeam> workTeams = JpaUtil.linq(WorkTeam.class)
                    .or()
                    .exists(WorkTeamMember.class)
                    .equalProperty("workTeamCode", "workTeamCode")
                    .equal("memberId", loginUsername)
                    .end()
                    .equal("teamLeader", loginUsername)
                    .end()
                    .list();
            if (!workTeams.isEmpty()){
                return workTeams.get(0);
            }
        }
        return null;
    }
}
